﻿using System;
using System.Windows;
using System.Windows.Forms;

namespace PhotoStartPageControl
{
    /// <summary>
    /// Interaction logic for BackgroundImageSettings.xaml
    /// </summary>
    public partial class BackgroundImageSettings : Window
    {
        #region Fields

        private BackgroundImageControl _backgroundImageControl;

        #endregion

        #region Properties

        private BackgroundImageControl BackgroundImageControl
        {
            get
            {
                return _backgroundImageControl;
            }
        }

        private ImageSource ImageSource
        {
            get
            {
                if (UseLocalImageRadioButton.IsChecked.GetValueOrDefault())
                    return ImageSource.LocalImage;

                if (UseOnlineImageRadioButton.IsChecked.GetValueOrDefault())
                    return ImageSource.OnlineImage;

                if (UseRssFeedRadioButton.IsChecked.GetValueOrDefault())
                    return ImageSource.RssFeed;

                return ImageSource.LocalImage;
            }
            set
            {
                switch ((int)value)
                {
                    case 0:     // Use Local File

                        UseLocalImageRadioButton.IsChecked = true;
                        break;

                    case 1:     // Use Online Image

                        UseOnlineImageRadioButton.IsChecked = true;
                        break;

                    case 2:     // Use RSS Feed

                        UseRssFeedRadioButton.IsChecked = true;
                        break;
                }
            }
        }

        #endregion

        #region Constructors

        public BackgroundImageSettings(BackgroundImageControl imageControl)
        {
            if (imageControl == null)
                throw new ArgumentNullException("The imageControl argument cannot be null.");

            InitializeComponent();

            _backgroundImageControl = imageControl;
            ImageSource = (ImageSource)BackgroundImageControl.ImageSource;
            FilePathTextBox.Text = BackgroundImageControl.FilePath;
            ImageUrlTextBox.Text = BackgroundImageControl.ImageUrl;
            FeedUrlTextBox.Text = BackgroundImageControl.RssFeedUrl;
            ImageUrlXmlNodeTextBox.Text = BackgroundImageControl.ImageUrlXmlNode;
            UseXmlAttributeCheckBox.IsChecked = new bool?(BackgroundImageControl.UseXmlAttribute);
            ImageUrlXmlAttributeTextBox.Text = BackgroundImageControl.ImageUrlXmlAttribute;
        }

        #endregion

        #region Event Handlers

        private void SaveSettingsButton_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult messageBoxResult = MessageBoxResult.OK;

            try
            {
                this.BackgroundImageControl.UpdateImageRssFeedInformation((int)ImageSource, FilePathTextBox.Text, ImageUrlTextBox.Text, FeedUrlTextBox.Text, ImageUrlXmlNodeTextBox.Text, UseXmlAttributeCheckBox.IsChecked.GetValueOrDefault(), ImageUrlXmlAttributeTextBox.Text);
            }
            catch (Exception ex)
            {
                messageBoxResult = System.Windows.MessageBox.Show(string.Format("{0}\n{1}", ex.Message, ex.StackTrace), "An error occurred while saving your settings.", MessageBoxButton.OKCancel);
            }
            finally
            {
                if (messageBoxResult != MessageBoxResult.Cancel)
                    this.Close();
            }
        }

        private void SelectFileButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();            
            dialog.Filter = "Image Files (*.bmp, *.jpg, *.jpeg, *.png, *.gif)|*.bmp;*.jpg;*.jpeg;*.png;*.gif;";
            dialog.Title = "Please select an image file.";

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                FilePathTextBox.Text = dialog.FileName;
        }

        #endregion
    }
}